require 'MVVM.Sources.View.Adaptor'

VerticalScrollListPosAdaptor = class('VerticalScrollListPosAdaptor', Adaptor)

function VerticalScrollListPosAdaptor:ctor(gameObject, scrollPositionName, binder, _func)
    self.component = gameObject:GetComponent('ScrollRect')
    binder:RegisterEvent(function(viewModel, property)
        self.component.onValueChanged:AddListener(function(pos)
            local normalizedPosition = self.component.verticalNormalizedPosition
            rawset(property, '_value', normalizedPosition)
        end)
    end, function()
        self.component.onValueChanged:RemoveAllListeners()
    end, scrollPositionName)
    self:BindProperty(scrollPositionName, binder)
    self.PosChangeFunc = _func
end

function VerticalScrollListPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    --if oldValue ~= newValue then
    if not newValue then return end
    coroutine.start(function ()
        coroutine.step()
        self.component.verticalNormalizedPosition = newValue
    end)
    --end
    if self.PosChangeFunc then
        self:PosChangeFunc(newValue)
    end
end

HorizontalScrollListPosAdaptor = class('HorizontalScrollListPosAdaptor', Adaptor)

function HorizontalScrollListPosAdaptor:ctor(gameObject, scrollPositionName, binder)
    self.component = gameObject:GetComponent('ScrollRect')
    binder:RegisterEvent(function(viewModel, property)
        self.component.onValueChanged:AddListener(function(pos)
            local normalizedPosition = self.component.horizontalNormalizedPosition
            rawset(property, '_value', normalizedPosition)
        end)
    end, function()
        self.component.onValueChanged = nil
    end, scrollPositionName)
    self:BindProperty(scrollPositionName, binder)
end

function HorizontalScrollListPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if oldValue ~= newValue then
        self.component.horizontalNormalizedPosition = newValue
    end
end

ChatScrollListPosAdaptor = class('ChatScrollListPosAdaptor', Adaptor)

--聊天防抖特供
function ChatScrollListPosAdaptor:ctor(transform, scrollPositionName, binder,parentView)
    self.component = transform:GetComponent('ScrollRect')
    self.transform = transform
    self.maskImage = self.transform:Find('Viewport'):GetComponent('Image')
    self:BindProperty(scrollPositionName, binder)
    self.parentView = parentView
end

function ChatScrollListPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    --print(debug.traceback('ChatScrollListPosAdaptor OnViewModelValueChanged '..tostring(newValue)))
    if not newValue then return end
    if not self.parentView.hasBind then return end
    if self.co then
        coroutine.stop(self.co)
        self.co = nil
    end
    self.co = coroutine.start(function ()
        self.maskImage.color = Color.New(1,1,1,0)
        coroutine.wait(0.2)
        self.component.verticalNormalizedPosition = newValue
        self.maskImage.color = Color.New(1,1,1,1)
    end)
end

VerticalScrollListAPosAdaptor = class('VerticalScrollListAPosAdaptor', Adaptor)

function VerticalScrollListAPosAdaptor:ctor(gameObject, scrollPositionName, binder)
    self.component = gameObject:GetComponent('ScrollRect')
    binder:RegisterEvent(function(viewModel, property)
        self.component.onValueChanged:AddListener(function(pos)
            local anchoredYPosition = self.component.content.transform.anchoredPosition.y
            rawset(property, '_value', anchoredYPosition)
        end)
    end, function()
        self.component.onValueChanged:RemoveAllListeners()
    end, scrollPositionName)
    self:BindProperty(scrollPositionName, binder)
end

function VerticalScrollListAPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if not newValue then return end

    self.view:StartCoroutine(function()
        UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.component.content.transform)
        coroutine.step()
        local anchoredPosition = self.component.content.transform.anchoredPosition
        anchoredPosition.y = newValue
        self.component.content.transform.anchoredPosition = anchoredPosition
    end)
end

HorizontalScrollListAPosAdaptor = class('HorizontalScrollListAPosAdaptor', Adaptor)

function HorizontalScrollListAPosAdaptor:ctor(gameObject, scrollPositionName, binder)
    self.component = gameObject:GetComponent('ScrollRect')
    binder:RegisterEvent(function(viewModel, property)
        self.component.onValueChanged:AddListener(function(pos)
            local anchoredXPosition = self.component.content.transform.anchoredPosition.x
            rawset(property, '_value', anchoredXPosition)
        end)
    end, function()
        self.component.onValueChanged:RemoveAllListeners()
    end, scrollPositionName)
    self:BindProperty(scrollPositionName, binder)
end

function HorizontalScrollListAPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if not newValue then return end

    self.view:StartCoroutine(function()
        UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.component.content.transform)
        coroutine.step()
        local anchoredPosition = self.component.content.transform.anchoredPosition
        anchoredPosition.x = newValue
        self.component.content.transform.anchoredPosition = anchoredPosition
    end)
end